package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.mapper.MessageMapper;
import cn.wolfcode.trip.base.query.MessageQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.log.LogInputStream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * MessageServiceImpl class
 *
 * @author lishuanwang
 * @date 2019/5/31
 */
@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public void save(Message message) {
        messageMapper.insert(message);
    }

    /**
     * 返回信息,如果有未读,全部返回,没有未读的话返回最近10条信息
     * @param qo, 请求对象
     * @return
     */
    @Override
    public PageInfo<Map> listAll(MessageQuery qo) {
        List<Map> list;
        qo.setOrderBy("m.create_time desc");
        list= messageMapper.selectForUnread(qo);
        if (list.size() == 0){
            qo.setPageSize(10);
            PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
            list = messageMapper.selectForList(qo);
        }

        return new PageInfo<>(list);
    }

    @Override
    public int getUnReadInfoNums(Long userId) {
        return 0;
    }

    @Override
    public void updateMessageRead(MessageQuery qo) {
        messageMapper.updateMessageRead(qo);
    }

    @Override
    public Integer getUnReadMessageNums(MessageQuery qo) {
        return messageMapper.getUnReadMessageNums(qo);
    }

    @Override
    public Integer getUserUnReadMessageNums(MessageQuery qo) {
        return messageMapper.getUserUnReadMessageNums(qo);
    }

    @Override
    public PageInfo getChatRoomMessage(Long groupId, QueryObject qo) {
        qo.setPageSize(10);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "create_time desc");
        List<Map> list = messageMapper.selectForChatRoom(groupId);
        return new PageInfo(list);
    }

}
